package web.controller.address_telefon;

import java.util.List;

import javax.faces.context.FacesContext;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.AddressBook;
import hibernate.util.HibernateUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

public class CreateNewAddressBookController {

	private static final Log logger = LogFactory.getLog(CreateNewAddressBookController.class);
	
	private String addressBookName;
	private User user;
	private AddressBook addressBook;
	
	public CreateNewAddressBookController() {
		// TODO Auto-generated constructor stub
		logger.info("asdasdasd asdasd");
	}

	public String createNewAddressBook(){
		
		String pageToRedirect = "";
		int userID;
		if(addressBookName == null || addressBookName.equals("")){
			logger.info("adres defteri adi girmediniz.");
			pageToRedirect = "createdNewAddressBookSuccess"; // addressBook with warning sayfasi acilacak.
			return pageToRedirect;
		}
		try {
			
			FacesContext context = FacesContext.getCurrentInstance();
			userID = ((User)context.getExternalContext().getSessionMap().get("user")).getId();
			
			addressBook = new AddressBook();
			addressBook.setAddressBookName(addressBookName);
			addressBook.setUserID(userID);
			//user.setTelephoneBook(addressBook);
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(AddressBook.class);
			criteria.add(Restrictions.eq("userID",userID));

			List<AddressBook> addressBookFromDB = criteria.list();
			
			Criteria criteria2 = session.createCriteria(User.class);
			criteria2.add(Restrictions.eq("id",userID));

			List<User> userFromDB = criteria2.list();
			user = (User)criteria2.list().get(0);
			if(addressBookFromDB == null || addressBookFromDB.size() == 0){
				Transaction transaction = session.beginTransaction();
				addressBook.setUser(user);
				session.save(addressBook);
				//user.setTelephoneBook(addressBook);
				//session.saveOrUpdate(user);
				session.update(user);
				transaction.commit();
				addressBookName = "";
				pageToRedirect = "createdNewAddressBookSuccess";
			} else{
				logger.info("Bu kullanici icin adres defteri tanimlanmistir!");
				//telephoneBookName = "";
				pageToRedirect = "createdNewAddressBookSuccess"; // addressBook with warning sayfasi acilacak.
			}
			
			
//			if(users != null && users.size() == 1){
//				TelephoneBook telephoneBook = (TelephoneBook)criteria.list().get(0);
//				user.setTelephoneBook(telephoneBook);
//				//user = (User)session.
//				session.clear();
//				session = HibernateUtil.getSessionFactory().openSession();
//				Transaction transaction = session.beginTransaction();
//				session.save(telephoneBook);
//				//session.saveOrUpdate(user);
//				//session.update(user);
//				transaction.commit();
//				pageToRedirect = "createdNewTelephoneBookSuccess";
//			}
			
		} catch (Exception exception) {
			logger.error("Yeni telefon defteri olusturulurken hata olustu :", exception);
		}
		
		return pageToRedirect;
		
	}
	
	public String backToMain(){
		
		return "backToMain";
		
	}

	public String getAddressBookName() {
		return addressBookName;
	}

	public void setAddressBookName(String addressBookName) {
		this.addressBookName = addressBookName;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public AddressBook getAddressBook() {
		return addressBook;
	}

	public void setAddressBook(AddressBook addressBook) {
		this.addressBook = addressBook;
	}
}
